<?php
class PromotionsController extends AppController {

	var $name = 'Promotions';
	
	
	function beforeFilter() {
    parent::BeforeFilter();   	
	//$this->Auth->allow('login','logout','userlist','add','confirmaccount');
	//$this->Auth->allow('*');
}

	function index() {
		$this->Promotion->recursive = 0;
		$this->paginate = array(
        'conditions' => array('Promotion.user_id' => $this->Auth->user('id')),
        'limit' => 10
    );
		$this->set('promotions', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid promotion', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('promotion', $this->Promotion->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
		$this->data['Promotion']['user_id']=$this->Auth->user('id');
			$this->Promotion->create();
			if ($this->Promotion->save($this->data)) {
				$this->Session->setFlash(__('The promotion has been saved', true));
				$this->redirect(array('controller' => 'Services','action' => 'index'));
			} else {
				$this->Session->setFlash(__('The promotion could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Promotion->User->find('list');
		$this->set(compact('users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid promotion', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Promotion->save($this->data)) {
				$this->Session->setFlash(__('The promotion has been saved', true));
				$this->redirect(array('controller' => 'Services','action' => 'index'));
			} else {
				$this->Session->setFlash(__('The promotion could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Promotion->read(null, $id);
		}
		$users = $this->Promotion->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for promotion', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Promotion->delete($id)) {
			$this->Session->setFlash(__('Promotion deleted', true));
			$this->redirect(array('controller' => 'Services','action'=>'index'));
		}
		$this->Session->setFlash(__('Promotion was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>